#!/bin/sh
# PCP QA Test No. 1700
# Exercise the bpftrace PMDA - single values
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.bpftrace

case `admin/whatami`
in
    *openSUSE\ Leap\ 15.*)
	_notrun "this test does not work on openSUSE 15.x"
	;;
esac

_pmdabpftrace_check
echo "tracepoint:syscalls:sys_enter_openat" >$tmp.tmp
_pmdabpftrace_check_probes $tmp.tmp

status=1       # failure is the default!

_prepare_pmda bpftrace
trap "_pmdabpftrace_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

_filter_json()
{
    tee -a $seq_full | \
    sed -E -e 's,"script_id": .*,"script_id": "SCRIPT_ID",g' \
           -e 's,"created_at": .*,"created_at": "CREATED_AT",g' \
           -e 's,"last_accessed_at": .*,"last_accessed_at": "LAST_ACCESSED_AT",g' \
           -e 's,"pid": .*,"pid": "PID",g' \
           -e 's,"username": .*,"username": "USERNAME",g' \
           -e 's,"data_bytes": .*,"data_bytes": DATA_BYTES,g'
}

_get_metric_value()
{
    tail -1 | sed -E -e 's/.*value "(.*)"/\1/'
}

# real QA test starts here
cat <<EOF | _pmdabpftrace_install
# Installed by PCP QA test $seq on `date`
[dynamic_scripts]
enabled = true
auth_enabled = false
EOF

echo "=== start bpftrace script ==="
./src/store_and_fetch bpftrace.control.register "// name: testscript
tracepoint:syscalls:sys_enter_openat { @ = count(); @scalar = 2; }" | pmjson | _filter_json
_pmdabpftrace_wait_for_value bpftrace.scripts.testscript.probes 2 15

echo "=== generating openat() syscall activity ==="
_pmdabpftrace_generate_openat_syscall_activity &
echo "wait 2 seconds to capture syscall activity..."
pmsleep 2

echo "=== check metadata metrics ==="
pminfo -dfmtT bpftrace.scripts.testscript.status
pminfo -dfmtT bpftrace.scripts.testscript.pid | _value_filter_nonzero
pminfo -dfmtT bpftrace.scripts.testscript.exit_code
pminfo -dfmtT bpftrace.scripts.testscript.error
pminfo -dfmtT bpftrace.scripts.testscript.probes | _value_filter_nonzero
pminfo -dfmtT bpftrace.scripts.testscript.code
pminfo -dfmtT bpftrace.scripts.testscript.data_bytes | _value_filter_nonzero

echo "=== check data metrics ==="
pminfo -dfmtT bpftrace.scripts.testscript.data.root | _value_filter_nonzero
pminfo -dfmtT bpftrace.scripts.testscript.data.scalar

echo "=== check general bpftrace metrics ==="
pminfo -dfmtT bpftrace.info.tracepoints | _value_filter_any
pminfo -dfmtT bpftrace.info.scripts

echo "=== scripts as JSON ==="
pminfo -dfmtT bpftrace.info.scripts_json | _value_filter_any
pminfo -f bpftrace.info.scripts_json | _get_metric_value | pmjson | _filter_json


_pmdabpftrace_remove

# check if pmdabpftrace process manager was shutdown correctly
sleep 10
cat $PCP_LOG_DIR/pmcd/bpftrace.log \
| tee -a $seq_full \
| grep -o "manager: shutdown pmdabpftrace process manager"


status=0
exit
